<?php

namespace app\common\model;

use think\Model;

class BaseModel extends Model
{
    const DB    = '';
    const TABLE = '';
    protected $args;

    public function __construct($data = [], $args = [])
    {
        $this->args = $args;
        parent::__construct($data);
    }

    public static function tableName($alias = null)
    {
        if (empty($alias)) {
            return static::DB . '.' . static::TABLE;
        } else {
            return static::DB . '.' . static::TABLE . ' ' . $alias;
        }
    }

    protected function args($key = '', $default = null)
    {
        if (empty($key)) {
            return $this->args;
        } elseif (isset($this->args[$key])) {
            return $this->args[$key];
        }
        return $default;
    }

    public static function getFields($setNull = true)
    {
        $list = static::getConnection()->getFields(static::TABLE);
        if ($setNull) {
            foreach ($list as $key => $value) {
                $list[$key] = null;
            }
        }
        return $list;
    }

    protected function setUpdateByAttr()
    {
        return session('name');
    }

    public static function getValue($field, $where, $expire = 300, $default = null)
    {
        $cn    = cache_name([static::tableName(), 'value', $field, $where]);
        $value = static::where($where)->cache($cn, $expire)->value($field, $default);
        return $value;
    }

}